package com.gdhuarui.mapper;

import com.gdhuarui.pojo.*;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;

@Repository
public interface AdminMapper {

    @Select({"select * from `config`"})
    ArrayList<Config> getConfigs();

    @Select({"select * from `news` where",
            "if(#{search} != '', title = #{search}, 1 = 1)",
            "order by id DESC"})
    Page<News> getNews(String search);

    @Insert({"insert into `news`(title, content, createTime, updateTime, remark, catalog, picture)",
            "values(#{title}, #{content}, #{createTime}, #{updateTime}, #{remark}, #{catalog}, #{picture})"})
    Integer addNewsAction(News news);

    @Delete({"delete from `news` where id = #{id}"})
    Integer deleteNews(Integer id);

    @Select({"select * from `news` where id = #{id}"})
    News selectNewsById(Integer id);

    @Update({"update `news` set title = #{title}, remark = #{remark}, catalog = #{catalog}, content = #{content}, picture = #{picture}, updateTime = #{updateTime} where id = #{id}"})
    boolean updateNewsAction(News news);

    @Select({"select * from `project` where",
            "if(#{search} != '', title = #{search}, 1 = 1)",
            "order by id DESC"})
    Page<Project> getProject(String search);

    @Insert({"insert into `project`(title, content, picture, createTime, updateTime)",
            "values(#{title}, #{content}, #{picture}, #{createTime} , #{updateTime})"})
    boolean addProjectAction(Project project);

    @Delete({"delete from `project` where id = #{id}"})
    int deleteProject(int id);

    @Select({"select * from `project` where id = #{id}"})
    Project selectProjectById(int id);

    @Update({"update `project` set title = #{title}, content = #{content}, picture = #{picture}, updateTime = #{updateTime} where id = #{id}"})
    boolean updateProjectAction(Project project);

    @Select({"select * from `partner` where",
            "if(#{search} != '', company = #{search}, 1 = 1)",
            "order by id DESC"})
    Page<Partner> getPartner(String search);

    @Insert({"insert into `partner`(company, url, picture, createTime, updateTime)",
            "values(#{company}, #{url}, #{picture}, #{createTime} , #{updateTime})"})
    boolean addPartnerAction(Partner partner);

    @Delete({"delete from `partner` where id = #{id}"})
    Integer deletePartner(Integer id);

    @Select({"select * from `partner` where id = #{id}"})
    Partner selectPartnerById(Integer id);

    @Update({"update `partner` set company = #{company}, url = #{url}, picture = #{picture}, updateTime = #{updateTime} where id = #{id}"})
    boolean updatePartnerAction(Partner partner);

    @Select({"select * from `product` where",
            "if(#{search} != '', title = #{search}, 1 = 1)",
            "order by id DESC"})
    Page<Product> getProduct(String search);

    @Insert({"insert into `product`(title, remark, isOpenA, android, isOpenI, ios, picture, createTime, updateTime)",
            "values(#{title}, #{remark}, #{isOpenA}, #{android}, #{isOpenI}, #{ios}, #{picture}, #{createTime} , #{updateTime})"})
    boolean addProductAction(Product product);

    @Delete({"delete from `product` where id = #{id}"})
    Integer deleteProduct(Integer id);

    @Select({"select * from `product` where id = #{id}"})
    Product selectProductById(Integer id);

    @Update({"update `product` set",
            "title = #{title},",
            "remark = #{remark},",
            "isOpenA = #{isOpenA},",
            "android = #{android},",
            "isOpenI = #{isOpenI},",
            "ios = #{ios},",
            "picture = #{picture},",
            "updateTime = #{updateTime}",
            "where id = #{id}"})
    boolean updateProductAction(Product product);

    @Update({"update `config`",
            "set value = case `key`",
            "when '${key}' then '${value}'",
            "end",
            "where `key` = '${key}'"})
    boolean saveConfig(String key, String value);

    @Select({"select * from `log` where",
            "if(#{search} != '', root = #{search}, 1 = 1)",
            "order by id DESC"})
    Page<Log> getLog(String search);

    @Insert({"insert into `log`(ip, os, browser, root, createTime)",
            "values(#{ip}, #{os}, #{browser}, #{root} , #{createTime})"})
    boolean recordLog(String ip, String os, String browser, String root, Timestamp createTime);
}
